//
//  HACDispatcherViewController.m
//  HACDispatcher
//
//  Created by Chengjie on 2019/4/24.
//  Copyright © 2019 Happy. All rights reserved.
//

#import "HACDispatcherViewController.h"
#import <HACRouter/HACRouter.h>
#import <HACDispatcher/UITableView+HACDispatcher.h>
#import <Masonry/Masonry.h>
#import "HACDemoSectionOne.h"
#import "HACDemoSectionTwo.h"
#import "HACSectionThree.h"
#import "HACDispatcherViewControllerManage.h"
#import "LJNHRuleSection.h"
#import "LJNHRuleTableViewCell.h"

@interface HACDispatcherViewController ()
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) HACDemoSectionOne *sectionOne;
@property (nonatomic, strong) HACDemoSectionTwo *sectionTwo;
@property (nonatomic, strong) HACSectionThree *sectionThree;
@property (nonatomic, strong) LJNHRuleSection *ruleSection;
@property (nonatomic, strong) HACDispatcherViewControllerManage *dataManage;
@end

@implementation HACDispatcherViewController

HAC_INIT_ROUTER(Dispatcher项目应用首页, dispatcher_home, HACDispatcherViewController) {
    self = [super init];
    if (self) {
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configUI];
    [self loadDataFromServer];
}

- (void)configUI {
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}

- (void)loadDataFromServer {
    __weak HACDispatcherViewController *weakSelf = self;
    [self.dataManage loadDataFromServerSuccess:^(NSArray<NSMutableArray<id<HACTableViewCellModelProtocol>> *> * _Nonnull resultData) {
        __strong HACDispatcherViewController *strongSelf = weakSelf;
        if (strongSelf) {
            
            strongSelf.sectionOne.cellModels = [resultData firstObject];
            strongSelf.sectionOne.rankingWeight = 0.1;
            NSMutableArray<LWXPrivateCustomerInfoIntegrityRuleCellModel *> *cellModels = [NSMutableArray array];
            for (NSInteger idx = 0; idx < 6; idx++) {
                LWXPrivateCustomerInfoIntegrityRuleCellModel *cellModel = [LWXPrivateCustomerInfoIntegrityRuleCellModel new];
                cellModel.title = [NSString stringWithFormat:@"标题%zd", idx];
                NSString *text1 = @"1、我是文本我是文本我是文本我是文本我是文本我是文本我是文本我是文本我是文本我是文本我是文本我是文本\n\n2、我是文本我是文本我是文本我是文本我是文本我是文本我是文本我是文本我是文本我\n\n3、是文本我是文本我是文本我是文本我是文本我是文本我是文本";
                cellModel.content = [text1 substringToIndex:(arc4random() % text1.length)];
                [cellModels addObject:cellModel];
            }
            cellModels.lastObject.hiddenBottomLine = YES;
            // 设置section
            self.ruleSection.cellModels = cellModels;
            self.ruleSection.rankingWeight = 0.2;
            [self.tableView.hac_dispatcher appendingImplementorArr:@[strongSelf.sectionOne,strongSelf.ruleSection]];

        }
    }];
}

#pragma mark -Getter

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.hac_forceUseDispatcher = YES;
        // 开启权重排序
        _tableView.hac_dispatcher.openWeightRanking = YES;
        [_tableView hac_setBelongToViewController:self];
    }
    return _tableView;
}

- (HACDemoSectionOne *)sectionOne {
    if (!_sectionOne) {
        _sectionOne = [HACDemoSectionOne new];
    }
    return _sectionOne;
}

- (HACDemoSectionTwo *)sectionTwo {
    if (!_sectionTwo) {
        _sectionTwo = [HACDemoSectionTwo new];
    }
    return _sectionTwo;
}

- (HACSectionThree *)sectionThree {
    if (!_sectionThree) {
        _sectionThree = [HACSectionThree new];
    }
    return _sectionThree;
}


- (LJNHRuleSection *)ruleSection {
    if (!_ruleSection) {
        _ruleSection = [LJNHRuleSection new];
    }
    return _ruleSection;
}

- (HACDispatcherViewControllerManage *)dataManage {
    if (!_dataManage) {
        _dataManage = [HACDispatcherViewControllerManage new];
    }
    return _dataManage;
}

- (void)dealloc {
    NSLog(@"%s", __func__);
}

@end
